package com.zeta.eduservice.service.impl;

import com.zeta.eduservice.entity.question.EduChapter;
import com.zeta.eduservice.entity.question.EduCoursequestionlistInfo;
import com.zeta.eduservice.entity.vo.Ata;
import com.zeta.eduservice.entity.vo.QuestionUploadVo;
import com.zeta.eduservice.mapper.EduCoursequestionlistInfoMapper;
import com.zeta.eduservice.mapper.EduQuestionChoiceMapper;
import com.zeta.eduservice.mapper.EduQuestionMapper;
import com.zeta.eduservice.service.EduChapterService;
import com.zeta.eduservice.service.EduCoursequestionlistInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程习题类型信息 服务实现类
 * </p>
 *
 * @author zeta
 * @since 2020-11-03
 */
@Service
public class EduCoursequestionlistInfoServiceImpl extends ServiceImpl<EduCoursequestionlistInfoMapper, EduCoursequestionlistInfo> implements EduCoursequestionlistInfoService {
	@Autowired
	EduChapterService chapterService;
	@Autowired
	EduQuestionChoiceMapper choiceMapper;
	@Autowired
	EduCoursequestionlistInfoService courseListService;
	@Autowired
	EduQuestionMapper questionMapper;
@Override
	public boolean updateCourseChoiceQuestionListInfo(QuestionUploadVo currChapterId) {
//		EduChapter byId = chapterService.getById(chapterId);
//
//		QueryWrapper<EduCoursequestionlistInfo> query = new QueryWrapper<EduCoursequestionlistInfo>();
//		query.eq("course_id", byId.getCourseId());
//		query.eq("type", "选择");
//		EduCoursequestionlistInfo one = new EduCoursequestionlistInfo();
//		EduCoursequestionlistInfo one2 = courseListService.getOne(query);
//		if (one2 != null) {
//			BeanUtils.copyProperties(one2, one);
//
//		}
//		Ata getinfo;
//		getinfo = choiceMapper.getinfo(byId.getCourseId());
//		if (getinfo != null) {
//			one.setNumber(getinfo.getCount()).setDifficulty(getinfo.getSum() / getinfo.getCount());
//		}
//		one.setCourseId(byId.getCourseId());
//		one.setType("选择");
//		return courseListService.saveOrUpdate(one);
	return true;
	}
@Override
public void updateCourseQuestionListInfo(String currChapterId,List<String> typelist){

	EduChapter byId = chapterService.getById(currChapterId);

	Ata questionTypeCount;
	QueryWrapper<EduCoursequestionlistInfo> query = new QueryWrapper<EduCoursequestionlistInfo>();
	query.eq("course_id", byId.getCourseId());
	List<EduCoursequestionlistInfo> list = courseListService.list(query);

	for (String i : typelist) {
		EduCoursequestionlistInfo one = new EduCoursequestionlistInfo();
		if (i != null) {
			questionTypeCount = questionMapper.QuestionTypeCount(byId.getCourseId(), i);
			for (EduCoursequestionlistInfo k : list) {
				if (k.getType().equals(i)) {
					QueryWrapper<EduCoursequestionlistInfo> qq = new QueryWrapper<EduCoursequestionlistInfo>();
					query.eq("course_id", byId.getCourseId());
					query.eq("type", k.getType());
					one = courseListService.getOne(qq);
					break;
				} 
			}
			one.setNumber(questionTypeCount.getCount());
			one.setType(i).setCourseId(byId.getCourseId())
					.setDifficulty(questionTypeCount.getSum() / questionTypeCount.getCount());
			courseListService.saveOrUpdate(one);
		}
	}
}
}
